/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.core.dao;

import acceso.hibernate.AQuery;
import acceso.hibernate.AccesoHibernate;
import acceso.util.Escritor;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import org.apache.commons.io.IOUtils;
import org.hibernate.StatelessSession;
import wf.dto.login.ppregsesfinDto;
import wf.dto.login.pppermis;
import wf.dto.login.ppregsesiniDto;
import wf3.core.ManagerQuery;
import wf3.hibernate.WQuery;
import wf3.log.Log;

/**
 *
 * @author romulogalindo
 */
public class LoginDAO extends DAO {

    StatelessSession session = null;
    AQuery query;
    int idsession = 0;

//    StatelessSession session;
//    ManagerQuery query;
//    private Log log = new Log(getClass());
    public LoginDAO() {
        this(0);
    }
    
    public LoginDAO(int idsession) {
        this.idsession =idsession;
        session = AccesoHibernate.getSessionFactory().openStatelessSession();
        query = new AQuery(this.idsession);
        idDAO = idsession;
        time = System.currentTimeMillis();
    }

    /**
     * @param p_usuari 
     * @param p_passwo 
     * @param p_modowf 
     * @param p_navega 
     * @param p_ipaddr 
     * @param p_cosyst 
     * @return  
     * @throws java.lang.Exception  
     */
    public ppregsesiniDto SESION(String p_usuari, String p_passwo, int p_modowf, String p_navega, String p_ipaddr, int p_cosyst) throws Exception {
        query.setMquery(session.getNamedQuery("sesion.ppregsesini"));
        query.setString("p_no_usuari", p_usuari);
        query.setString("p_no_passwo", p_passwo);
        query.setString("p_no_ip", p_ipaddr);
        query.setString("p_no_macadr", "");
        query.setString("p_no_sisnav", p_navega);
        query.setInteger("p_ti_frawor", p_modowf);
//        q.setInteger("p_co_system", p_cosyst);
        ppregsesiniDto ppregses = (ppregsesiniDto) query.uniqueResult();
        return ppregses;
    }

    /**
     * 
//     * @param p_modowf
     * @param p_id_sesion
     * @param p_co_sistem
     * @return 
     * @throws java.lang.Exception 
     */
    public pppermis PERMIS(int p_id_sesion, short p_co_sistem) throws Exception {
        query.setMquery(session.getNamedQuery("sesion.pppermis"));
        query.setInteger("p_id_session", p_id_sesion);
        query.setShort("p_co_sistem", p_co_sistem);
        pppermis permis = (pppermis) query.uniqueResult();
        return permis;
    }

    
    /**
     * 
     * @param p_nomurl
     * @param p_modowf
     * @param p_sesion
     * @param p_co_sistem
     * @return 
     * @throws java.lang.Exception 
     */
    public pppermis PERMIS_OVER_MASTER(String p_nomurl, int p_modowf, int p_sesion, short p_co_sistem) throws Exception {
        System.out.println("http://" + p_nomurl + "/wl?xact=4&p_modowf=" + p_modowf + "&p_sesion=" + p_sesion + "&p_co_sistem=" + p_co_sistem);
        String m_url = "http://" + p_nomurl + "/wl?xact=4&p_modowf=" + p_modowf + "&p_sesion=" + p_sesion + "&p_co_sistem=" + p_co_sistem;
        org.apache.http.HttpResponse response = null;
        org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
        org.apache.http.client.methods.HttpGet httpget = new org.apache.http.client.methods.HttpGet(m_url);
        try {
            response = client.execute(httpget);
            org.apache.http.HttpEntity entity = response.getEntity();
            InputStream instream = entity.getContent();
            pppermis dto = new Gson().fromJson(IOUtils.toString(instream, "ISO-8859-1"), pppermis.class);

            return dto;
        } catch (IOException | IllegalStateException | JsonSyntaxException ep) {
            System.out.println("ep:" + ep.getMessage());
            ep.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @param id_sesion
     * @return 
     * @throws java.lang.Exception
     */
    public ppregsesiniDto SESION(long id_sesion) throws Exception {
        query.setMquery(session.getNamedQuery("sesion.ppregsesini_2"));
        query.setInteger("p_id_sesion", Integer.parseInt("" + id_sesion));
        ppregsesiniDto ppregses = (ppregsesiniDto) query.uniqueResult();
        return ppregses;
    }

    /**
     * 
     * @param p_nomurl
     * @param id_sesion
     * @return 
     * @throws java.lang.Exception
     */
    public ppregsesiniDto SESION_OVER_MASTER(String p_nomurl, long id_sesion) throws Exception {
        System.out.println("http://" + p_nomurl + "/wl?xact=3&id_sesion=" + id_sesion);
        String m_url = "http://" + p_nomurl + "/wl?xact=3&id_sesion=" + id_sesion;
        org.apache.http.HttpResponse response = null;
        org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
        org.apache.http.client.methods.HttpGet httpget = new org.apache.http.client.methods.HttpGet(m_url);
        try {
            response = client.execute(httpget);
            org.apache.http.HttpEntity entity = response.getEntity();
            InputStream instream = entity.getContent();
            ppregsesiniDto dto = new Gson().fromJson(IOUtils.toString(instream, "ISO-8859-1"), ppregsesiniDto.class);

            return dto;
        } catch (IOException | IllegalStateException | JsonSyntaxException ep) {
            System.out.println("ep:" + ep.getMessage());
            ep.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @param id_sesion
     * @throws java.lang.Exception
     */
    public void ACTUALIZAR(int id_sesion) throws Exception {
        query.setMquery(session.getNamedQuery("sesion.ppregsesfin"));
        query.setInteger("p_id_sesion", id_sesion);
        query.uniqueResult();
    }

    @Override
    public void CLOSE_SESSION() {
        close = true;
        session.close();
        session = null;
        query = null;
        Escritor.escribe_debug("[" + idDAO + "] LoginDAO-> cerrado.");
    }

    @Override
    public long getIdDAO() {
        return idDAO;
    }

    @Override
    public long getTimelife() {
        return time;
    }

    @Override
    public boolean isClosed() {
        return close;
    }
}
